﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace fabao.Common.Util
{
    public class DateTimeHelper
    {
        /// <summary>
        /// 获取两个日期的天数差
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        public static int GetDateDiff(DateTime startTime, DateTime endTime)
        {
            DateTime start = Convert.ToDateTime(startTime.ToShortDateString());
            DateTime end = Convert.ToDateTime(endTime.ToShortDateString());
            TimeSpan sp = end.Subtract(start);
            return sp.Days;
        }

        /// <summary>
        /// 获取两个时间相差的秒数
        /// </summary>
        /// <param name="startTime"></param>
        /// <param name="endTime"></param>
        /// <returns></returns>
        public static int GetDateDiffSeconds(DateTime startTime, DateTime endTime)
        {
            TimeSpan sp = endTime - startTime;
            return (int)sp.TotalSeconds;
        }

        /// <summary>
        /// 将两个时间格式差值转换成00:00分钟格式
        /// </summary>
        /// <param name="startTime">开始时间</param>
        /// <param name="endTime">结束时间</param>
        /// <returns></returns>
        public static string CalculateTimeDifference(DateTime startTime, DateTime endTime)
        {
            if (endTime < startTime)
                return "00:00";

            TimeSpan timeDifference = endTime - startTime;

            // 将时间差转换成分钟和秒
            int totalSeconds = (int)timeDifference.TotalSeconds;
            int minutes = totalSeconds / 60;
            int seconds = totalSeconds % 60;

            return $"{minutes}:{seconds}";
        }
        /// <summary>
        /// 转换DateTime类型时间为时间戳
        /// </summary>
        /// <param name="dateTime">DateTime类型时间</param>
        /// <param name="millisecond">是否精确到毫秒，默认值为false</param>
        /// <returns>long型时间戳</returns>
        public static long ConvertToTimestampLong(DateTime dateTime, bool millisecond = false)
        {
            TimeSpan ts = dateTime.ToUniversalTime() - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            return millisecond ? Convert.ToInt64(ts.TotalMilliseconds) : Convert.ToInt64(ts.TotalSeconds);
        }
    }
}
